import { TOKEN_KEY } from '../constants/token-const'

const token = wx.getStorageSync('TOKEN_KEY')

const BASE_URL = 'http://123.207.32.32:9001' 
const LOGIN_BASE_URL = 'http://123.207.32.32:3000'

class SWRequest{
  //这里也要传入header 
  constructor(baseURL,authHeader={}){
    this.baseURL = baseURL
    this.authHeader = authHeader
  }
     // isAuth 这个变量记录着是否要授权去获取token ，默认是不授权的
  request(url,method,params,isAuth=false,header={}){
    // header不一定只会传来一个，可能还有别的，所以进行拼接
    const finalHeader = isAuth? {...this.authHeader , ...header} : header
    return new Promise((resolve,reject)=>{
      wx.request({
          url: this.baseURL + url,
          method:method,
          data:params,
          header:finalHeader,
          success:function(res){
            resolve(res.data)
          },
          fail: reject
        })
    })
  }
  get(url,params,header,isAuth=false){
    return this.request(url,"GET",params,header,isAuth)
  }
  post(url,data,header,isAuth=false){
    return this.request(url,"POST",data,header,isAuth)
  }
}
const swRequest = new SWRequest(BASE_URL)
// 传入header携带token
const swLoginRequest = new SWRequest(LOGIN_BASE_URL,{token})

export default swRequest
export{swLoginRequest} 

